package com.hmall.search.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.hmall.api.dto.ItemDTO;
import com.hmall.search.domain.dto.PageDTO;
import com.hmall.search.domain.query.ItemPageQuery;
import com.hmall.search.service.ISearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "搜索相关接口")
@RestController
@RequestMapping("/search")
@RequiredArgsConstructor
public class SearchController {

    private final ISearchService searchService;
    private final SpringUtil springUtil;

    @ApiOperation("搜索商品")
    @GetMapping("/list")
    public PageDTO<ItemDTO> search(ItemPageQuery query)  throws Exception {

        PageDTO<ItemDTO> result = searchService.search(query);

        // 封装并返回
        return result;
    }

    @ApiOperation("搜索的过滤项")
    @PostMapping("/filters")
    public Map<String, List<String>> filters(@RequestBody ItemPageQuery query)  throws Exception {

        Map<String, List<String>> result = searchService.filters(query);

        // 封装并返回
        return result;
    }

    @ApiOperation("初始化ES中的数据")
    @GetMapping("/initES")
    public String search()  throws Exception{

        searchService.initES();

        return "SUCCESS";
    }
}
